package xiaobange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import xiaobange.common.R;
import xiaobange.dto.SetmealDto;
import xiaobange.entity.Category;
import xiaobange.entity.Dish;
import xiaobange.entity.Setmeal;
import xiaobange.entity.SetmealDish;
import xiaobange.service.CategoryService;
import xiaobange.service.DishService;
import xiaobange.service.SetmealDishService;
import xiaobange.service.SetmealService;
import xiaobange.service.imp.SetmealServiceImp;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> SetmealPage(int page,int pageSize,String name){

        //构造分页构造器
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //设置查询条件
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null , Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        setmealService.page(setmealPage,lambdaQueryWrapper);

        //构建一个新的分页构造器
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //拷贝刚刚分页构造器的数据，或略掉records属性
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //去除records属性，重新给他赋值
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> collect = records.stream().map((item) -> {
            //new一个新的SetmealDto实体类
            SetmealDto setmealDto = new SetmealDto();
            //因为SetmealDto每new一次都是空的，所以吧遍历出来的数据付给他
            BeanUtils.copyProperties(item, setmealDto);
            //取出ID
            Long itemId = item.getCategoryId();
            //根据ID进行查询
            Category category = categoryService.getById(itemId);
            //判断根据ID查询是否查询到数据
            if (category != null){
                //把查询到的名称赋给SetmealDto里的categoryName属性
                setmealDto.setCategoryName(category.getName());
            }
            //返回处理好的数据
            return setmealDto;
        }).collect(Collectors.toList());//将返回的数据遍历成list集合

        //把处理好的数据付给新的分页查询构造器
        setmealDtoPage.setRecords(collect);

        return R.success(setmealDtoPage);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "SetCache" , allEntries = true)
    @PostMapping
    public R<String> AddFormSubmit(@RequestBody SetmealDto setmealDto){
        setmealService.Insert(setmealDto);
        return R.success("添加成功");
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(value = "SetCache" , allEntries = true)
    @DeleteMapping
    public R<String> Delete(Long[] ids){
        setmealService.DeleteByID(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{State}")
    public R<String> UpdateState(@PathVariable int State, Long[] ids){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId , ids);
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        List<Setmeal> collect = list.stream().map((item) -> {
            item.setStatus(State);
            return item;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(collect);
        return R.success("修改成功！");
    }

    /**
     * 修改数据回显
     * @param ID
     * @return
     */
    @GetMapping("/{ID}")
    public R<SetmealDto> Update(@PathVariable Long ID){
        SetmealDto setmealDto = setmealService.UpdateShow(ID);
        return R.success(setmealDto);
    }


    /**
     * 修改数据提交
     * @param setmealDto
     * @return
     */
    @PutMapping
    private R<String> UpdateSubmit(@RequestBody SetmealDto setmealDto){
        setmealService.UpdateSubmit(setmealDto);
        return R.success(null);
    }

    /**
     * 查看套餐
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "SetCache" , key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<SetmealDto>> listR(Setmeal setmeal){
        //构造查询条件
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getStatus() != null ,Setmeal::getStatus , setmeal.getStatus());
        lambdaQueryWrapper.eq(setmeal.getCategoryId() != null , Setmeal::getCategoryId , setmeal.getCategoryId());
        Setmeal one = setmealService.getOne(lambdaQueryWrapper);
        if (one == null){
            R.error("套餐当前套餐状态停售中!");
        }
        //查询套餐内的菜品
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(SetmealDish::getSetmealId , one.getId());
        List<SetmealDish> list = setmealDishService.list(dishLambdaQueryWrapper);

        //给套餐内的菜品图片赋值
        List<SetmealDto> setmealDtoList = list.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Dish dish = dishService.getById(item.getDishId());
            setmealDto.setImage(dish.getImage());
            return setmealDto;
        }).collect(Collectors.toList());

        return R.success(setmealDtoList);
    }



}
